﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PIM_ATE
{
    public partial class FrmConfigRCUData : Form
    {
        public FrmConfigRCUData(ClsDataRcuConfigBuilder builder)
        {
            InitializeComponent();
            m_cfgBuilder = builder;

        }

        ClsDataRcuConfigBuilder m_cfgBuilder;
        public ClsDataRcuConfig rsconfig;
        DataTable m_rcuconfig;

        List<ClsDataRcuConfigUnit> m_lstRCUConfig = new List<ClsDataRcuConfigUnit>();

        private void FrmConfigRCUData_Load(object sender, EventArgs e)
        {
            var resultObj = new ClsDataResultForAntennaBuilder(GDefine.DBNamePIM);
            string[] lstName = resultObj.getProcodeListFromTMO();
            if (lstName != null)
            {
                cbProcode.Items.AddRange(lstName);
            }
            cbProcode.Visible = true;
            text_procode.Visible = false;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToAddRows = false;
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            if(Convert.ToInt32(text_module_index.Text) > Convert.ToInt32(text_module_count.Text))
            {
                MessageBox.Show("模块序号超出模块数量!");
                return;
            }
            if (Convert.ToInt32(text_unit_index.Text) > Convert.ToInt32(text_unit_count.Text))
            {
                MessageBox.Show("单元号超出单元数量!");
                return;
            }
            if(!text_port_count.Text.Contains("&"))
            {
                MessageBox.Show("端口号输入格式错误!");
                return;
            }
            //将各RCUUnit参数添加入列表

            var Rconfig = new ClsDataRcuConfigUnit();
            Rconfig.speed = Convert.ToInt32(text_speed.Text);
            Rconfig.unit_count = Convert.ToInt32(text_unit_count.Text);
            Rconfig.unit_index = Convert.ToInt32(text_unit_index.Text);
            Rconfig.port_count = text_port_count.Text;
            Rconfig.port_name = text_port_name.Text;
            Rconfig.moduleSn_length = Convert.ToInt32(text_moduleSn_length.Text);
            Rconfig.port_location = Convert.ToInt32(text_port_location.Text);
            m_lstRCUConfig.Add(Rconfig);

            
            m_rcuconfig = toTable(m_lstRCUConfig);
            dataGridView1.DataSource = m_rcuconfig;

        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                //不变参数的赋值
                rsconfig = new ClsDataRcuConfig();
                rsconfig.name = text_name.Text;
                rsconfig.fileName = text_filename.Text;
                //rsconfig.procode = text_procode.Text;
                rsconfig.procode = cbProcode.Text;
                rsconfig.procodeType = text_procodeType.Text;
                rsconfig.uid_count = Convert.ToInt32(text_uid_count.Text);
                rsconfig.uid_length = Convert.ToInt32(text_uid_length.Text);
                rsconfig.module_count = Convert.ToInt32(text_module_count.Text);
                rsconfig.module_index = Convert.ToInt32(text_module_index.Text);
                rsconfig.unit = m_lstRCUConfig;
                //变化参数unit里的赋值
                //for (int i = 0; i < lstunit_index.Count; i++)
                //{
                //    var Rconfig = new ClsDataRcuConfigUnit();
                //    Rconfig.speed = Convert.ToInt32(lstspeed[i]);
                //    Rconfig.unit_count = Convert.ToInt32(lstunit_count[i]);
                //    Rconfig.unit_index = Convert.ToInt32(lstunit_index[i]);
                //    Rconfig.port_count = lstport_count[i];
                //    Rconfig.port_name = lstport_name[i];
                //    Rconfig.moduleSn_length = Convert.ToInt32(lstmoduleSn_length[i]);
                //    Rconfig.port_location = Convert.ToInt32(lstport_location[i]);
                //    m_lstRCUConfig.Add(Rconfig);

                //}

                
                if (m_cfgBuilder == null)
                    this.DialogResult = DialogResult.OK;
                else
                {
                    //数据库上传
                    string error = "";
                    if (m_cfgBuilder.upload(rsconfig.procode, "1.0", rsconfig, ref error))
                    {
                        MessageBox.Show("上传成功");
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("上传失败:" + error);
                    }
                }
               

            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("保存参数错误");
                return;
            }
        }

        private List<string> getRCUConfigColNames()
        {
            List<string> names = new List<string>();
            names.Add("单元数量");
            names.Add("单元号");
            names.Add("端口号");
            names.Add("端口名称");
            names.Add("天线序列号长度");
            names.Add("端口字符位置");
            names.Add("出厂转速");
            
            return names;
        }

        private DataTable toTable(List<ClsDataRcuConfigUnit> lstConfig)
        {
            List<string> cols = getRCUConfigColNames();
            DataTable dt = new DataTable();
            for (int i = 0; i < cols.Count; i++)
            {
                dt.Columns.Add(cols[i]);
            }

            for (int n = 0; n < lstConfig.Count; n++)
            {
                int i = 0;
                DataRow dr = dt.NewRow();
                //dr[i++] = lstConfig[n].Enable;
                dr[i++] = lstConfig[n].unit_count;
                dr[i++] = lstConfig[n].unit_index;

                dr[i++] = lstConfig[n].port_count;
                dr[i++] = lstConfig[n].port_name;
                dr[i++] = lstConfig[n].moduleSn_length;
                dr[i++] = lstConfig[n].port_location;
                dr[i++] = lstConfig[n].speed;
               
                dt.Rows.Add(dr);
            }
            return dt;
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            if (m_lstRCUConfig != null)
                m_lstRCUConfig.Clear();
            m_rcuconfig = toTable(m_lstRCUConfig);
            dataGridView1.DataSource = m_rcuconfig;
        }

        private void text_speed_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
